#include "Deck.h"

enum resultOfCompare {LOWER = -1, EQUAL = 0, GREATER = 1};

void Deck::add(Card* extraCard){
  contents.push_back(extraCard);
}

void Deck::add(Deck* extraDeck){
  for(int i = 0; i < extraDeck->size(); i++)
  {
    contents.push_back(extraDeck->contents.at(i));
  }
}

Card* Deck::at(int position){
  assert(position >= 0);
  assert(position < contents.size());
  return contents.at(position);
}

Card* Deck::draw(){
  if(contents.size() != 0){
    Card* drawTemp = *contents.begin();
    contents.erase(contents.begin());
    return drawTemp;  
  }
  else
    return NULL;
}

Deck Deck::draw(int deckSize){
  /* Reduce deckSize so you won't take more cards than there are. */
  if(deckSize > size()) deckSize = size();
  /* Create a place to store the drawn cards. */
  Deck result;
  /* Keep drawing as needed. */
  for(int i = 0; i < deckSize; i++)
  {
    result.add(draw()); 
  }
  return result;
}

void Deck::debugPrint()
{
  for(int i = 0; i < contents.size(); ++i)
  {
    contents.at(i)->debugPrint();
    cout << " ";
  }
  cout << endl;
}

void Deck::remove(Card* target)
{
  int i;
  for(int i = 0; i < contents.size(); ++i)
  {
    if(contents.at(i) == target)
    {
      contents.erase(std::remove(contents.begin(), contents.end(),
      contents.at(i)), contents.end());
    }
  }
}

void Deck::replace(int index, Card* card)
{
  assert(index >= 0);
  assert(index < contents.size());
  contents.at(index) = card;
}

int Deck::size(){
  return contents.size();
}

void Deck::shuffle(){
  /* Don't shuffle single cards or empty decks. */
  if(contents.size() == 0 || contents.size() == 1) return;
  /* Fisher-Yates shuffle. */
  for(int i = 0; i < contents.size()-1; i++)
  {
    int j = i + rand() % (contents.size()-i);
    /* Swap. */
    Card* swapTemp = contents.at(i);
    contents.at(i) = contents.at(j);
    contents.at(j) = swapTemp;
  }
}

void Deck::sort()//untested code! Needs Testing!
{
  int tempLargest=-9999; // TEST DIS 
  for (int i=0; i<contents.size(); i++){
    tempLargest=-9999;
    for (int j=0; j<contents.size();j++){
      if (tempLargest<contents.at(j)->getRank()){
        tempLargest=contents.at(j)->getRank();
        }
      if (tempLargest>contents.at(j)->getRank()){
        Card* tempswitch=contents.at(j-1);
        contents.at(j-1) = contents.at(j);
        contents.at(j) = tempswitch;
      }
    }
  }
}

void Deck::transfer(int numberOfCards, Deck* recipient){
  /* Take cards from this deck... */
  Deck addMe = draw(numberOfCards);
  /* ...and give them to recipient. */
  recipient->add(&addMe);
}
